/**
 * <p> GPS功能开启关闭的实现类
 * @Title: GpsDevicceOperation.java
 * @Package com.rushswise.lovetrip.gps
 * @Description: 提供三个接口来操作GPS的开启和关闭
 * Copyright: Copyright (c) 2013-2013 All rights reserved
 * Company: 
 * 
 * @author Rush Song-Administrator
 * @date 2013-8-10 下午8:25:02
 * @version V1.0
 */
package com.rushswise.lovetrip.gps;

import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.util.Log;

import com.rushswise.lovetrip.LoveTripMainActivity;
import com.rushswise.lovetrip.commoninterface.DeviceFunOperation;
import com.rushswise.lovetrip.commoninterface.DeviceType;

/**
 * <p> the implementation class to operate GPS function open/close status.
 * @ClassName: GpsDevicceOperation
 * @Description: TODO
 * @author Rush Song-Administrator
 * @date 2013-8-10 下午8:25:02
 *
 */
public class GpsDevicceOperation implements DeviceFunOperation
{

    /* (non-Javadoc)
     * @see com.rushswise.lovetrip.commoninterface.DeviceFunOperation#getOperationType()
     */
    @Override
    public String getOperationType()
    {
        return DeviceType.ANDROID_GPS.toString();
    }

    /* (non-Javadoc)
     * @see com.rushswise.lovetrip.commoninterface.DeviceFunOperation#openDeviceFunciton()
     */
    @Override
    public boolean openDeviceFunciton()
    {
        if(isActive())
        {
            return true;
        }
        return toggleGPS();
    }

    /* (non-Javadoc)
     * @see com.rushswise.lovetrip.commoninterface.DeviceFunOperation#closeDeviceFunction()
     */
    @Override
    public boolean closeDeviceFunction()
    {
        if(!isActive())
        {
            return true;
        }
        return toggleGPS();
    }

    /* (non-Javadoc)
     * @see com.rushswise.lovetrip.commoninterface.DeviceFunOperation#isActive()
     */
    @Override
    public boolean isActive()
    {
        /*
         * 用Setting.System来读取也可以，只是这是更旧的用法 String str =
         * Settings.System.getString(getContentResolver(),
         * Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
         */
        String str = Settings.Secure.getString(
                LoveTripMainActivity.obtainContentResolver(),
                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        Log.v("GPS", str);
        if (str != null)
        {
            return str.contains("gps");
        }
        else
        {
            return false;
        }
    }

    /**
      * operate GPS, if open then close and reverse
      *
      * @Title: toggleGPS
      * @Description: TODO
      * @param 
      * @return void
      * @throws
      */
    private boolean toggleGPS()
    {
        Intent gpsIntent = new Intent();
        gpsIntent.setClassName("com.android.settings",
                "com.android.settings.widget.SettingsAppWidgetProvider");
        gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
        gpsIntent.setData(Uri.parse("custom:3"));
        
        Context currContext = LoveTripMainActivity.getCurrActivity();
        if(null == currContext)
        {
            Log.v("Error", "Can get current Activity.");
            return false;
        }
        boolean opearteStatus = true;
        try
        {
            PendingIntent.getBroadcast(currContext, 0, gpsIntent, 0).send();
        }
        catch (CanceledException e)
        {
            Log.v("Exception in operate GPS", e.getMessage());
            opearteStatus = false;
        }
        
        return opearteStatus;
    }


    
}
